import tkinter as tk
from tkinter import filedialog
import os

root = tk.Tk()
root.geometry("500x500")
root.title("dir2xyz")


def choose_dir():
    folder_selected = filedialog.askdirectory()
    print(folder_selected)
    show_info.set(folder_selected)
#     if not os.path.exists(folder_selected):
#         return
    file = open(folder_selected+os.sep+os.path.basename(folder_selected)+"_gps.bat","w",encoding="utf-8")
    myFiles = os.listdir(folder_selected)
    for imgName in myFiles:
        if(imgName.endswith(".JPG") and len(imgName.split("-"))>=3):
            xyzArr = imgName.replace(".JPG","").split("-")
            lon_str ="114.0"+str(12000-int(xyzArr[0])).zfill(5)
            lat_str = "30.5"+xyzArr[1]
            heigh =1.45-int(xyzArr[2])/1000
            line_str = "exiftool -P -overwrite_original -GPSLongitudeRef=E -GPSLongitude=" + lon_str
            line_str += " -GPSLatitudeRef=N -GPSLatitude=" + lat_str
            line_str += " -GPSAltitudeRef=Above -GPSAltitude=" + str(heigh)
            line_str += " " + imgName + os.linesep
            file.write(line_str)
    file.close()
    show_info.set("操作成功！")


button = tk.Button(root, text="Select image dir", command=choose_dir)
button.pack(side="top", pady=50)

show_info = tk.StringVar()
panel = tk.Label(root, text='图片路径:', textvariable=show_info)
panel.pack(side="top", expand="yes", padx="50", pady="50")

root.mainloop()